package com.studentmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.our.learning.infrastructure.constant.ResponseStatusEnum;
import com.our.learning.infrastructure.po.Clazz;
import com.our.learning.infrastructure.pojo.Response;
import com.studentmanagement.mapper.ClassMapper;
import com.studentmanagement.mapper.StudentMapper;
import com.studentmanagement.po.StudentPO;
import com.studentmanagement.service.ClassService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author hexujie
 * @date 2023-06-21 14:30
 */
@Service
public class ClassServiceImpl implements ClassService {

    private final ClassMapper classMapper;
    private final StudentMapper studentMapper;

    public ClassServiceImpl(ClassMapper classMapper, StudentMapper studentMapper) {
        this.classMapper = classMapper;
        this.studentMapper = studentMapper;
    }

    @Override
    public Response createClass(Clazz clazz) {
        if (classMapper.insert(clazz) != 1) {
            return new Response(ResponseStatusEnum.DATABASE_ERROR);
        }
        return new Response(ResponseStatusEnum.OK);
    }

    @Override
    public Response modifyClass(Clazz clazz) {

        if (classMapper.selectById(clazz.getId()) == null) {
            return new Response(ResponseStatusEnum.BAD_REQUEST)
                    .setMsg("班级不存在");
        }

        if (classMapper.updateById(clazz) != 1) {
            return new Response(ResponseStatusEnum.DATABASE_ERROR);
        }
        return new Response(ResponseStatusEnum.OK)
                .addData("class", clazz.getId());
    }

    @Override
    public Response classList(Integer classId) {
        QueryWrapper<Clazz> queryWrapper = new QueryWrapper<>();
        if (classId != null) {
            queryWrapper.eq("id", classId);
        }
        List<Clazz> clazzList = classMapper.selectList(queryWrapper);

        return new Response(ResponseStatusEnum.OK)
                .addData("clazzList", clazzList);
    }

    @Override
    public Response addStudentToClass(Integer stuId, Integer classId) {
        StudentPO studentPO = studentMapper.selectById(stuId);
        if (studentPO == null) {
            return new Response(ResponseStatusEnum.BAD_REQUEST)
                    .setMsg("学生不存在");
        }
//        studentPO.setStudentId(stuId);
        Clazz clazz = classMapper.selectById(classId);
        if (clazz == null) {
            return new Response(ResponseStatusEnum.BAD_REQUEST)
                    .setMsg("班级不存在");
        }
        if (studentPO.getClassId() != clazz.getId()) {
            clazz.setStuNum(clazz.getStuNum() + 1);
            studentPO.setClassId(classId);
            if (studentMapper.updateById(studentPO) != 1 || classMapper.updateById(clazz) != 1) {
                return new Response(ResponseStatusEnum.DATABASE_ERROR);
            }
        }

        return new Response(ResponseStatusEnum.OK);
    }

    @Override
    public Response removeStudentFromClass(Integer stuId, Integer classId) {
        StudentPO studentPO = studentMapper.selectById(stuId);
        if (studentPO == null) {
            return new Response(ResponseStatusEnum.BAD_REQUEST)
                    .setMsg("学生不存在");
        }
        studentPO.setStudentId(stuId);
        Clazz clazz = classMapper.selectById(classId);
        if (clazz == null) {
            return new Response(ResponseStatusEnum.BAD_REQUEST)
                    .setMsg("班级不存在");
        }
        if (studentPO.getClassId() == clazz.getId()) {
            clazz.setStuNum(clazz.getStuNum() - 1);
            studentPO.setClassId(null);
        }
        if (studentMapper.updateById(studentPO) != 1 || classMapper.updateById(clazz) != 1) {
            return new Response(ResponseStatusEnum.DATABASE_ERROR);
        }

        return new Response(ResponseStatusEnum.OK);
    }
}
